﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FrontRoomCheck.Utiltiy;

namespace FrontRoomCheck.ShowDataForm
{
	public class SelectFloorManager
	{
		public List<Level> m_AllLevels
		{
			get
			{
				return this.allLevels;
			}
			set
			{
				this.allLevels = value;
			}
		}

		public bool IfFrontRoomCheck
		{
			get
			{
				return this.m_bIfFrontRoomCheck;
			}
		}

		public bool IfEvacuationDistance
		{
			get
			{
				return this.m_bIfEvacuationDistance;
			}
			set
			{
				this.m_bIfEvacuationDistance = value;
			}
		}

		public bool SelectFloorLevels(Document doc, bool tmpTag)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			return this.SelectFloors(doc, tmpTag, ref list, null);
		}

		public bool SelectFloors(Document doc, bool tmpTag, ref List<ViewPlan> selectFloors, string strFormCaption = null)
		{
			List<Level> AllLevels = SelectFloorManager.GetSortLevels(doc, double.MinValue, double.MaxValue);
			StaticRescource.m_AllLevels = AllLevels;
			List<ViewPlan> list = new List<ViewPlan>();
			List<double> list2 = new List<double>();
			List<Level> list3 = new List<Level>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(ViewPlan));
			foreach (Element element in filteredElementCollector)
			{
				ViewPlan viewPlan = element as ViewPlan;
                if (viewPlan != null && (int)viewPlan.ViewType == 1 && viewPlan.GenLevel != null)
				{
					list2.Add(viewPlan.GenLevel.Elevation);
					list.Add(viewPlan);
				}
			}
			int i = 0;
			List<ViewPlan> list4 = new List<ViewPlan>();
			while (i < AllLevels.Count)
			{
				int num = list2.FindIndex((double item) => SelectFloorManager.IsEqual(item, AllLevels[i].Elevation));
				if (num == -1)
				{
					AllLevels.RemoveAt(i);
					i--;
				}
				else
				{
					list4.Add(list[num]);
				}
				i++;
			}
			SelectFloorForm selectFloorForm = new SelectFloorForm(list4, tmpTag, this.IfEvacuationDistance);
			selectFloorForm.strFormCaption = strFormCaption;
			if (selectFloorForm.ShowDialog() == DialogResult.OK)
			{
				return false;
			}
			if (selectFloorForm.IsCommit)
			{
				foreach (int index in selectFloorForm.SelectedViewIndexs())
				{
					selectFloors.Add(list4[index]);
				}
				foreach (ViewPlan viewPlan2 in selectFloors)
				{
					list3.Add(viewPlan2.GenLevel);
				}
				this.allLevels = list3;
				this.m_bIfFrontRoomCheck = selectFloorForm.m_IsReFindTag;
				return true;
			}
			return false;
		}

		public static List<Level> GetSortLevels(Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(Level)).ToElements();
			foreach (Element element in collection)
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (elevation >= bottom && elevation <= top)
				{
					list.Add(level);
				}
			}
			list.Sort(new SelectFloorManager.LevelComparer());
			return list;
		}

		private static bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private static bool IsEqual(double val1, double val2)
		{
			return !SelectFloorManager.LessThan(val1, val2) && !SelectFloorManager.LessThan(val2, val1);
		}

		private const double _epsDouble = 1E-09;

		private bool m_bIfFrontRoomCheck;

		private bool m_bIfEvacuationDistance;

		private List<Level> allLevels = new List<Level>();

		private class LevelComparer : IComparer<Level>
		{
			public int Compare(Level x, Level y)
			{
				return x.Elevation.CompareTo(y.Elevation);
			}
		}
	}
}
